package com.happybluefin.tapburstfree.window;

import com.happybluefin.tapburstfree.R;
import com.happybluefin.tapburstfree.common.tencentweibo.TencentWeibo;
import com.tencent.weibo.api.TAPI;
import com.tencent.weibo.constants.OAuthConstants;
import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.webview.OAuthV2AuthorizeWebView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class TencentWeiboWindow extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d(TAG, "onCreate() start");

        setContentView(R.layout.tencentweibowindow);

        Intent intent = getIntent();
        mText =intent.getStringExtra("TEXT"); 
        
        TencentWeibo weibo = new TencentWeibo(this);
        weibo.share(mText);

        Log.d(TAG, "onCreate() start");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.d(TAG, "onActivityResult() start");

        switch (requestCode) {
        case TencentWeibo.TENCENT_WEIBO_REQUEST_CODE:
            switch (resultCode) {
            case OAuthV2AuthorizeWebView.RESULT_CODE:
                try {
                    OAuthV2 oAuth = (OAuthV2)data.getExtras().getSerializable("oauth");
                    TAPI tapi = new TAPI(OAuthConstants.OAUTH_VERSION_2_A);
                    tapi.add(oAuth, "json", mText, "127.0.0.1");
                    tapi.shutdownConnection();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            default:
                break;
            }
            break;

        default:
            break;
        }
        
        finish();
        
        Log.d(TAG, "onActivityResult() end");
    }

    private String mText = null;

    private static final String TAG = "TencentWeiboWindow";
}